<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医院名称" prop="hospitalName">
        <el-input v-model="queryParams.hospitalName" placeholder="请输入医院名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="医院类型" prop="hospitalType">

        <el-select v-model="queryParams.hospitalType" placeholder="请选择医院类型" clearable size="small" style="width: 240px">
          <el-option key="" label="全部" value="" />
          <el-option key="1" label="医院" value="1" />
          <el-option key="2" label="卫生院" value="2" />
        </el-select>

      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="hospitalList">

      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="医院名称" prop="hospitalName" />
      <el-table-column label="医院类型" prop="hospitalType">
        <template slot-scope="scope">

          <el-tag v-if="scope.row.hospitalType==1">医院</el-tag>
          <el-tag type="success" v-else>卫生院</el-tag>

        </template>
      </el-table-column>

      <el-table-column label="医院地址" prop="hospitalAddress" width="400" show-overflow-tooltip />

      <el-table-column label="开始-结束经纬度" width="300">
        <template slot-scope="scope">
          <span>{{scope.row.latitude||'0'}},{{scope.row.longitude||'0'}}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" prop="createTime" width="150" />

      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" icon="el-icon-setting" v-hasPermi="['hospital:info:edit']" @click="handleAddSelect(scope.row)">坐标选取</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.params.pageNum" :limit.sync="queryParams.params.pageSize" @pagination="getList" />

    <!-- 获取地图坐标 -->
    <el-dialog top="5vh" title="地图选点" :visible.sync="openAddressSelect" width="1000px" append-to-body>
      <el-row>
        <span class="dialog-mb20">选择的经纬度：{{marker.position}} 位置：{{marker.address}}</span>
        <div class="amap-page-container">
          <el-amap :vid="'amap-vue'" :zoom="zoom" :center="center" :events="events">
            <el-amap-marker :position="marker.position" :events="marker.events" :visible="marker.visible" :draggable="marker.draggable"></el-amap-marker>
          </el-amap>
        </div>

      </el-row>
      <span slot="footer" class="dialog-footer dialog-footer-top">
        <el-button type="primary" @click="submitAddSelect">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { listHospital, updateHospital } from "@/api/areaSystem/hospitalGISManage";

export default {
  name: "hospitalGISManage",

  data() {
    const _this = this
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地区表格数据
      menuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,


      //1.开始地图选点、结束地图选点
      openAddressSelect: false,

      position: '',


      //医院列表
      hospitalList: [],


      // 查询参数
      queryParams: {
        params: {
          pageNum: 1,
          pageSize: 10,
        },
        //医院名称
        hospitalName: '',
        // 医院类型
        hospitalType: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        //	医院名称
        "hospitalName": [
          { required: true, message: "医院名称不能为空", trigger: "blur" },
          { min: 4, max: 40, message: "医院名称必须是4-40位", trigger: "blur" }
        ],

      },


      zoom: 14,
      center: [104.63446, 35.00238],
      marker: {
        position: [104.63446, 35.00238],
        address: '',
        visible: true,
        draggable: false
      },
      events: {
        click: (e) => {
          this.marker.position = [e.lnglat.lng, e.lnglat.lat]
          // 这里通过高德 SDK 完成。
          var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: 'all'
          })
          geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], function (status, result) {
            if (status === 'complete' && result.info === 'OK') {
              if (result && result.regeocode) {
                _this.marker.address = result.regeocode.formattedAddress
                _this.$nextTick()
              }
            }
          })

          console.log("object", _this.marker);

        }
      },

    };
  },
  created() {

    //医院列表
    this.getList();
  },
  methods: {

    //医院列表
    async getList() {
      this.loading = true;
      const res = await listHospital(this.queryParams)
      //医院列表
      this.hospitalList = res.rows;
      this.total = res.total;
      this.loading = false;
      console.log("this.hospitalList", this.hospitalList);
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pageNum: 1,
        pageSize: 10,
        //医院名称
        hospitalName: '',
        // 医院类型
        hospitalType: '',

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        params: {
          pageNum: 1,
          pageSize: 10,
        },
        //医院名称
        hospitalName: '',
        // 医院类型
        hospitalType: '',
      }
      this.getList();
    },


    //开始坐标、结束坐标
    handleAddSelect(rows) {

      console.log("rows,style", rows);

      //当前行数据获取
      this.addressSelectItem = rows



      // 弹出层标题
      this.addressSelectTitle = "坐标选点"


      this.mapCenter = this.center
      this.lng = this.center[0]
      this.lat = this.center[1]

      // 开始地图选点、结束地图选点 弹出层标题
      this.openAddressSelect = true


    },

    // 选点提交
    async submitAddSelect() {

      console.log("当前行数据获取", this.addressSelectItem);
      console.log("当前位置", this.marker);


      this.addressSelectItem.latitude = this.marker.position[0]
      this.addressSelectItem.longitude = this.marker.position[1]
      this.addressSelectItem.hospitalAddress = this.marker.address


      const res = await updateHospital(this.addressSelectItem)

      if (res.code !== 200) return this.$message.error(res.msg);


      this.msgSuccess('位置修改完成');
      // 开始地图选点、结束地图选点 弹出层标题
      this.openAddressSelect = false

      //更新完成，拉取最新列表
      this.getList();

    }


  }
};
</script>

<style lang="less" scoped>
.mb {
  margin-bottom: 20px;
}
.dialog-mb20 {
  height: 35px;

  display: inline-block;
  font-size: 16px;
}
.dialog-footer-top {
  margin-top: 10px;
  position: relative;
}

.amap-page-container {
  width: 100%;
  height: 450px;
}

.search-box {
  margin-top: 0px;
  position: relative;
  top: 65px;
  left: 20px;
}
</style>